package com.hsh.utils.pdf;

import com.google.common.collect.Lists;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hsh on 17-3-6.
 */
public class PdfUtils {

    private static String PDF_PATH = "/root/Downloads/source.pdf";
    private static String BOOK_MARK_TEXT_PATH = "/root/Downloads/list.txt";
    private static String PDF_SAVED_FILENAME = "dest.pdf";

    private static int offset = 14;//pdf和目录角标补偿

    public static void main(String[] args) {
        addBookMark(PDF_PATH, BOOK_MARK_TEXT_PATH);
    }

    public static void addBookMark(String pdfPath, String bookMarkTextPath) {
        try {
            File file = new File(pdfPath);
            String savedPdfPath = file.getParent() + "/" + PDF_SAVED_FILENAME;

            List<String> data = IOUtils.readLines(new FileInputStream(bookMarkTextPath));
            if (data == null || data.isEmpty()) {
                System.out.println("目录为空");
                return;
            }

            FileInputStream inputStream = new FileInputStream(file);
            PdfReader pdf = new PdfReader(inputStream);
            List<HashMap<String, Object>> bookmarks = Lists.newArrayList();

            HashMap<String, Object> currMap = null;
            String pattern = "^[\\d.]\\s*(.*)$";
            for (String s : data) {
                s = s.trim();
                Pattern p = Pattern.compile(pattern);
                Matcher matcher = p.matcher(s);

                if (matcher.find()) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("Action", "GoTo");
                    Mark mark = getMark(s);
                    map.put("Title", mark.getTitle());
                    map.put("Page", mark.getPager() + " Fit");
                    if (currMap.get("Kids") == null) {
                        currMap.put("Kids", Lists.newArrayList());
                    }
                    ((List) currMap.get("Kids")).add(map);
                } else {
                    //第几章
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("Action", "GoTo");
                    Mark mark = getMark(s);
                    map.put("Title", mark.getTitle());
                    map.put("Page", mark.getPager() + " Fit");
                    bookmarks.add(map);
                    currMap = map;
                }
            }

            for (int i = 0; i < bookmarks.size(); i++) {
                HashMap<String, Object> bm = bookmarks.get(i);
                System.out.println(bm);
            }


            PdfStamper stamper = new PdfStamper(pdf, new FileOutputStream(savedPdfPath));
            stamper.setOutlines(bookmarks);
            stamper.close();

            IOUtils.closeQuietly(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Mark getMark(String line) {
        Mark mark = new Mark();
        String title = line;
        int pager = 1;
        for (int i = line.length() - 1; i >= 0; i--) {
            char c = line.charAt(i);
            if (c >= '0' && c <= '9') {

            } else {
                title = line.substring(0, i + 1);
                try {
                    pager = (Integer.parseInt(line.substring(i + 1)) + offset);
                } catch (Exception e) {
                }
                break;
            }
        }
        mark.setPager(pager);
        mark.setTitle(title);
        return mark;
    }


    private static class Mark {
        private String title;
        private int pager;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getPager() {
            return pager;
        }

        public void setPager(int pager) {
            this.pager = pager;
        }
    }
}
